package com.hly.product.service.impl;

import com.hly.product.vo.AttrAttrGroupRelationSaveVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.common.utils.PageUtils;
import com.hly.common.utils.Query;

import com.hly.product.dao.AttrAttrgroupRelationDao;
import com.hly.product.entity.AttrAttrgroupRelationEntity;
import com.hly.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * AttrId集合保存数据
     * @param saveVo
     */
    @Transactional
    @Override
    public void saveByAttrIds(AttrAttrGroupRelationSaveVo saveVo) {
        List<AttrAttrgroupRelationEntity> collect = saveVo.getAttrIds().stream().map(attrId -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrGroupId(saveVo.getGroupId());
            entity.setAttrId(attrId)    ;
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);
    }

    /**
     * 根据attrId删除数据
     * @param asList
     */
    @Transactional
    @Override
    public void deleteByAttrId(List<Long> asList) {
        this.remove(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_id",asList));
    }

}